﻿//-----------------------------------------------------------------------
// <copyright file="ExceptionDataContractUnitTest.cs" company="CodePlex">
//     Copyright (c) Doug Holland, CodePlex. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
namespace CodePlex.Diagnostics.Contracts
{
    using System;
    
    using Microsoft.VisualStudio.TestTools.UnitTesting;

    /// <summary>
    /// Provides unit tests for the <see cref="T:CodePlex.Diagnostics.Contracts.ExceptionDataContract"/> class.
    /// </summary>
    [TestClass]
    public sealed class ExceptionDataContractUnitTest
    {
        #region public ExceptionDataContractUnitTest()

        /// <summary>
        /// Initializes a new instance of the <see cref="T:ExceptionDataContractUnitTest"/> class.
        /// </summary>
        public ExceptionDataContractUnitTest()
        {
        }

        #endregion

        #region public TestContext TestContext

        /// <summary>
        /// Gets or sets the test context which provides information about and functionality for the current test run.
        /// </summary>
        public TestContext TestContext
        {
            get;
            set;
        }

        #endregion

        #region public void ExceptionDataContractConstructorUnitTest()

        /// <summary>
        /// Ensures that the constructor of the <see cref="T:CodePlex.Diagnostics.Contracts.ExceptionDataContract"/> class is correctly copying the values from the supplied <see cref="T:System.Exception"/> instance.
        /// </summary>
        [TestMethod]
        public void ExceptionDataContractConstructorUnitTest()
        {
            try
            {
                for (int index = 0; index < 10; index++)
                {
                    index /= index;
                }
            }
            catch (DivideByZeroException ex)
            {
                // establish an instance of the invalid operation exception that contains the divide by zero exception as an inner-exception.
                InvalidOperationException invalidOperationException = new InvalidOperationException(ex.Message, ex);

                // establish an instance of the data contract based upon the invalid operation exception.
                ExceptionDataContract exception = new ExceptionDataContract(invalidOperationException);

                Assert.IsFalse(string.IsNullOrEmpty(exception.SerializedException), "data contract contains an invalid soap encoded exception instance.");
                
                Assert.IsNotNull(exception.DeserializedException, "an error occurred deserializing the soap encoded exception instance.");

                Assert.AreEqual<string>(exception.HelpLink, invalidOperationException.HelpLink, "data contract contains an invalid help link value.");

                Assert.IsNotNull(exception.HResult, "data contract contains an invalid HResult value.");

                Assert.AreEqual<string>(exception.Message, invalidOperationException.Message, "data contract contains an invalid message value.");

                Assert.AreEqual<string>(exception.Source, invalidOperationException.Source, "data contract contains an invalid source value.");

                Assert.AreEqual<string>(exception.StackTrace, invalidOperationException.StackTrace, "data contract contains an invalid stack trace value.");
                
                // ensure that the inner exception collection is not null.
                Assert.IsNotNull(exception.InnerExceptions);

                foreach (ExceptionDataContract innerException in exception.InnerExceptions)
                {
                    Assert.AreEqual<string>(innerException.HelpLink, ex.HelpLink, "data contract contains an invalid help link value.");

                    Assert.IsNotNull(innerException.HResult, "data contract contains an invalid HResult value.");

                    Assert.AreEqual<string>(innerException.Message, ex.Message, "data contract contains an invalid message value.");

                    Assert.AreEqual<string>(innerException.Source, ex.Source, "data contract contains an invalid source value.");

                    Assert.AreEqual<string>(innerException.StackTrace, ex.StackTrace, "data contract contains an invalid stack trace value.");
                }
            }
        }

        #endregion
    }
}
